Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. While loops can be thought of as iterative versions of if statements. When we need to repeatedly run a block of statements, Java's while loop is used. While loops are viewed as iterative if statements.
Use of the while loop is advised if the number of iterations is not fixed.
Flow Chart
Syntax
while (condition) {
// Statement to executed when condition true
}
// Java Loop program using while loop
public class Student { public static void main(String[] args) { int i = 1; System.out.println("Print the first 10 natural numbers "); while(i<=10) { System.out.println(i); i = i + 1; } } }
Output:
Print the first 10 natural numbers
1
2
3
4
5
6
7
8
9
10
// Java Loop program using while loop
public class whileLoop { public static void main(String[] args) { while(true){ System.out.println("Infinitive while loop"); } } }
Output:
Infinitive while loop
Infinitive while loop
Infinitive while loop
Infinitive while loop
Press Ctrl+C
Post your comment